package cn.highedu.coolsharkhub.controller;

import cn.highedu.coolsharkhub.service.exception.BlankParameterException;
import cn.highedu.coolsharkhub.service.exception.EntityNotFoundException;
import cn.highedu.coolsharkhub.service.exception.UserNotFoundException;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.bind.annotation.RestControllerAdvice;

/**
 * 全局异常处理器
 * 用于处理所有控制器抛出的异常
 * @author HighEDU
 */
@RestControllerAdvice
public class GlobalExceptionHandler {

    /**
     * 处理EntityNotFoundException异常
     * @param e EntityNotFoundException异常
     * @return 异常信息
     */
    @ExceptionHandler(EntityNotFoundException.class)
    @ResponseStatus(HttpStatus.NOT_FOUND) // 404
    public String handleEntityNotFoundException(EntityNotFoundException e) {
        return e.getMessage();
    }

    /**
     * 处理UserNotFoundException异常
     * @param e UserNotFoundException异常
     * @return 异常信息
     */
    @ExceptionHandler(UserNotFoundException.class)
    @ResponseStatus(HttpStatus.NOT_FOUND) // 404
    public String handleUserNotFoundException(UserNotFoundException e) {
        return e.getMessage();
    }

    /**
     * 处理BlankParameterException异常
     * @param e BlankParameterException异常
     * @return 异常信息
     */
    @ExceptionHandler(BlankParameterException.class)
    @ResponseStatus(HttpStatus.BAD_REQUEST) // 400
    public String handleBlankParameterException(BlankParameterException e) {
        return e.getMessage();
    }

    /**
     * 处理其他Exception异常
     * @param e Exception异常
     *          未知异常
     *          服务器内部错误
     *          500
     * @return 异常信息
     */
    @ExceptionHandler(Exception.class)
    @ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR) // 500
    public String handleException(Exception e) {
        return e.getMessage();
    }
}
